//
//  BadgeCollectionViewCell.swift
//  ANOA
//
//  Created by 邱仙凯 on 2022/9/6.
//  Copyright © 2025 aowang. All rights reserved.
//

import UIKit

// 让UICollectionViewCell 拥有红色角标功能
class BadgeCollectionViewCell: UICollectionViewCell {
    
    
    var badgeNum: Int? {
        didSet {
            if let badgeNum = self.badgeNum {
                
                guard badgeNum > 0 else {
                    badgeNumLabel.isHidden = true
                    return
                    
                }
                self.contentView.bringSubviewToFront(badgeNumLabel)
                let tatal = badgeNum < 100 ? String(badgeNum) : "99⁺"
                
                badgeNumLabel.isHidden = false
                badgeNumLabel.text = tatal
            } else {
                badgeNumLabel.isHidden = true
            }
        }
    }
    
    lazy var badgeNumLabel:UILabel = {
        let v = UILabel(frame:CGRect(x: self.contentView.frame.maxY - 26, y: 6, width: 20, height: 20))
        v.isHidden = true
        v.backgroundColor = UIColor.red
        v.textAlignment = .center
        v.textColor = UIColor.white
        v.font = UIFont.systemFont(ofSize: 11)
        v.layer.cornerRadius = 10
        v.layer.masksToBounds = true
        return v
        
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.contentView.addSubview(badgeNumLabel)
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
